<?php
class ome_order_fail{
    public function batchModifyOrder(&$cursor_id,$params){
        $oldPbn = $params['sdfdata']['oldPbn'];
        $pbn = $params['sdfdata']['pbn'];
        foreach($params['sdfdata']['orderId'] as $val){
            kernel::single("ome_order_fail")->modifyOrderItems($val,$oldPbn,$pbn);
        }
        return false;
    }

    public function modifyOrder($order_id){
        $orderObj = &app::get('ome')->model('orders');

        $order = $orderObj->dump($order_id,'*',array('order_objects'=>array('*',array('order_items'=>array('*')))));
        if ($order['is_fail'] == 'true'){
            foreach($order['order_objects'] as $obj=>$items){
                foreach($items['order_items'] as $key=>$item){
                    if($item['product_id']<=0 || !isset($item['product_id'])){
                        $data = array('edit_status'=>'true');
                        $orderObj->update($data,array('order_id' =>$order_id));
                        return false;
                    }
                }
            }
        
            //修正订单
            $orderData['is_fail'] = 'false';
            $orderObj->update($orderData,array('order_id' =>$order_id));

          //创建订单后执行的操作
        	if($oServiceOrder = kernel::servicelist('ome_fail_modify_order_after')){
			   foreach(kernel::servicelist('ome_fail_modify_order_after') as $object){
			      if(method_exists($object,'modify_order_after')){
                      $object->modify_order_after($order);
             	  }
			   }
       	    }
        }
        return true;
    }

    public function modifyOrderItems($order_id,$oldPbn,$pbn){
        $productObj = &app::get('ome')->model('products');
        $goodsObj = &app::get('ome')->model('goods');
        $orderObj = &app::get('ome')->model('orders');
        $itemObj = &app::get('ome')->model('order_items');
        $objectsObj = &app::get('ome')->model('order_objects');

        $data = array('edit_status'=>'false');
        $orderObj->update($data,array('order_id' =>$order_id));

        //对货品进行过滤更新
        if($oldPbn && $pbn){
            foreach($pbn as $key=>$bn){
                if($bn){
                	$is_pkg=false;
                	//先查找捆绑商品 如果存在删掉原来货品 插入捆绑货品，更新商品
                	$pkg_goods = &app::get('omepkg')->model('pkg_goods');
     				$pkg_product = &app::get('omepkg')->model('pkg_product');
     				
     				$itemInfo = $itemObj->dump(array('order_id'=>$order_id,'item_id'=>$key));
     	// var_dump($itemInfo);
     	//echo $itemInfo['obj_id'];
     				//取得商品
     				//$goods = $objectsObj->dump(array('obj_id'=>$itemInfo['obj_id ']));
     				$goods = $objectsObj->dump(array('order_id'=>$order_id,'obj_id'=>$itemInfo['obj_id']));
     				//var_dump($goods);
     				$old_goods_id=$goods['goods_id'];
                	$goods_id=$pkg_goods->getPkgBn($bn);
                	//echo $goods_id;
                	if($goods_id){
                		$is_pkg=true;
                		$goods_id=$goods_id['goods_id'];
                		$goods['goods_id']=$goods_id;
                		$goods['obj_type']='pkg';
                		$goods['obj_alias']='捆绑商品';
                		$goods['bn']=$bn;
                		$productList=$pkg_product->getproduct($goods_id);
                		
                		foreach($productList as $k=>$value){
                			$array = $productObj->dump(array('bn'=>$value['bn']));
                			$add_mess = array();
                			$add_mess['shop_goods_id'] = '0';
                			$add_mess['product_id'] = $array['product_id'];
                			$add_mess['shop_product_id'] = '0';
                			$add_mess['bn'] = $array['bn'];
                			$add_mess['name'] = $array['name'];
                			$add_mess['cost'] = '0';
                			$add_mess['price'] ='0';
                			$add_mess['amount'] = '0';
                			$add_mess['weight'] = $array['weight'];
                			$add_mess['quantity'] = $goods['quantity']*$value['pkgnum'];//商品数量*捆绑数量
                			$add_mess['sendnum'] = '0';
                			$add_mess['addon'] = null;
                			$add_mess['item_type'] = 'pkg';
                			$add_mess['score'] = '0';
                			$add_mess['obj_id'] = $itemInfo['obj_id'];
                			$add_mess['order_id'] = $order_id;
                			//处理冻结库存
                			$productObj->chg_product_store_freeze($array['product_id'],(intval($add_mess['quantity'])),"+");
                			// var_dump($add_mess)	;
                			//保存item
                			$itemObj->save($add_mess);//更新货品item
                		}
                		//删除原货品
                		$itemObj->delete(array('item_id'=>$key));
                		$objectsObj->save($goods);//更新商品
                		unset($goods);
                	}else{                	
                	//直接更新货品和商品  
                    	$product = $productObj->dump(array('bn'=>$bn),'product_id,bn,goods_id,name');
                    	$goods=$goodsObj->dump($product['goods_id'],'goods_id,bn,name');
                    	
                    	if($product){
                    		$item = array(
                    				'product_id'=>$product['product_id'],
                    				'bn'=>$product['bn'],
                    		);

                    		$itemInfo = $itemObj->dump(array('order_id'=>$order_id,'item_id'=>$key));
                    		$productObj->chg_product_store_freeze($item['product_id'],(intval($itemInfo['quantity'])-intval($itemInfo['sendnum'])),"+");

                    		$itemObj->update($item,array('item_id'=>$key));//更新货品item
                    		unset($item);
                    		$objectsObj->update($goods,array('order_id'=>$order_id,'goods_id'=>$goods['goods_id']));//更新商品object
                    		unset($goods);
                    	}
                    	
                    }

                   
                }
            }
        }
//echo 1111111;
        //修正为正常订单
        if(kernel::single("ome_order_fail")->modifyOrder($order_id)){
            return true;
        }else{
            return false;
        }
    }
}